# -*- coding: utf-8 -*-

from bs4 import BeautifulSoup
from ..base_parser import BasePageParser


class FcdListParser(BasePageParser):
    """新闻列表页解析器"""
    
    def can_handle(self, response):
        """判断是否为新闻列表页"""
        # 只处理列表页，不处理详情页
        return response.url == 'https://www.nist.gov/el/fcd'
    
    def parse(self, response):
        next_page_type = 'nist_project_detail'

        self.logger.info(f"正在解析列表页: {response.url}")
        
        soup = BeautifulSoup(response.text, 'html.parser')
        
        tr_list = soup.select('table tbody tr')
        
        print(f"找到 {len(tr_list)} 个链接")
        
        for tr in tr_list:
            link = tr.select_one('a')
            href = link.get('href')
            if href:
                detail_url = self.urljoin(response.url, href)
                print(f"找到链接: {detail_url}")
                
                yield self.make_request(
                    detail_url,
                    callback=self.spider.parse,  # 返回主spider进行路由
                    meta={
                        'category_url': response.url,
                        'page_type': next_page_type,
                        # 'categorys': ['Home', 'Engineering Laboratory', 'Fire Research Division']
                    }
                ) 